package ro.sergiu.finder.ui.button.customizers;

import bsh.EvalError;
import bsh.Interpreter;
import java.awt.Color;
import java.awt.Dimension;
import java.util.logging.Level;
import java.util.logging.Logger;
import junit.framework.Assert;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNotSame;
import org.junit.Before;
import org.junit.Test;
import ro.sergiu.finder.models.frame.FrameModel;
import ro.sergiu.finder.models.frame.FrameModelPropertyEditor;
import ro.sergiu.finder.models.grid.GridModel;
import ro.sergiu.finder.models.grid.GridModelPropertyEditor;
import ro.sergiu.finder.models.property.AutoresizeMode;
import ro.sergiu.finder.models.property.DisplayMode;
import static ro.sergiu.finder.models.property.PropertyEditorGenerator.DEFAULT;
import ro.sergiu.finder.models.property.SelectionMode;

/**
 *
 * @author Toporjinschi
 */
public class SettingsTestings {

    private GridModelPropertyEditor gridModelEditor;
    private FrameModelPropertyEditor frameModelEditor;

    @Before
    public void setUp() {
        gridModelEditor = new GridModelPropertyEditor() {
            @Override
            public Object getValue() {
                GridModel gridModel = new GridModel();
                gridModel.setAutoresizeMode(AutoresizeMode.AUTO_RESIZE_OFF);
                gridModel.setSelectionMode(SelectionMode.SINGLE_SELECTION);
                gridModel.setMovableColumns(false);
                gridModel.setSortableRecords(false);
                gridModel.setEvenBackground(Color.RED);
                gridModel.setEvenForeground(Color.BLACK);
                gridModel.setEvenForeground(Color.GRAY);
                gridModel.setOddBackground(Color.YELLOW);
                gridModel.setOddForeground(Color.GREEN);
                return gridModel;
            }
        };

        frameModelEditor = new FrameModelPropertyEditor() {
            @Override
            public Object getValue() {
                FrameModel frameModel = new FrameModel();
                frameModel.setTitle("test");
                frameModel.setResizable(true);
                frameModel.setMaximumSize(new Dimension(100, 100));
                frameModel.setMinimumSize(new Dimension(100, 100));
                frameModel.setPreferredSize(new Dimension(100, 100));
                frameModel.setSize(new Dimension(100, 100));
                frameModel.setDisplayMode(DisplayMode.POPUP);
                return frameModel;
            }
        };
    }

    @Test
    public void testGridModelPropertyEditor() {
        String genCode = gridModelEditor.getJavaInitializationString();
        GridModel testedObj = (GridModel) gridModelEditor.getValue();
        GridModel defObj = new GridModel();
        Interpreter interpreter = new Interpreter();
        try {
            System.out.println("Result tableSettings:\n" + genCode);
            interpreter.eval(genCode);
            GridModel compiled = (GridModel) interpreter.eval(genCode);
            assertEquals(compiled.getAutoresizeMode(), testedObj.getAutoresizeMode());
            assertEquals(compiled.getSelectionMode(), testedObj.getSelectionMode());
            assertEquals(compiled.getEvenBackground(), testedObj.getEvenBackground());
            assertEquals(compiled.getEvenForeground(), testedObj.getEvenForeground());
            assertEquals(compiled.getOddBackground(), testedObj.getOddBackground());
            assertEquals(compiled.getOddForeground(), testedObj.getOddForeground());

            assertNotSame(compiled.getAutoresizeMode(), defObj.getAutoresizeMode());
            assertNotSame(compiled.getSelectionMode(), defObj.getSelectionMode());
            assertNotSame(compiled.getEvenBackground(), defObj.getEvenBackground());
            assertNotSame(compiled.getEvenForeground(), defObj.getEvenForeground());
            assertNotSame(compiled.getOddBackground(), defObj.getOddBackground());
            assertNotSame(compiled.getOddForeground(), defObj.getOddForeground());
        } catch (EvalError ex) {
            Logger.getLogger(SettingsTestings.class.getName()).log(Level.SEVERE, null, ex);
            Assert.fail("Uncompilable code");
        }
    }

    @Test
    public void testGridModelGetText() {
        GridModelPropertyEditor gridModelE = new GridModelPropertyEditor();
        String text = gridModelE.getAsText();
        assertEquals(text, DEFAULT);
    }

    @Test
    public void testFrameModelGetText() {
        FrameModelPropertyEditor frameModelE = new FrameModelPropertyEditor();
        String text = frameModelE.getAsText();
        assertEquals("Probably setValue(null) is not called in constructor;", text, DEFAULT);
    }

    @Test
    public void testGetDefaultValue() {
        assertNotNull(new FrameModelPropertyEditor().getDefaultValue());
        assertNotNull(new GridModelPropertyEditor().getDefaultValue());
    }

    @Test
    public void testFrameModelPropertyEditor() {
        String genCode = frameModelEditor.getJavaInitializationString();
        FrameModel testedObj = (FrameModel) frameModelEditor.getValue();
        FrameModel defObj = new FrameModel();
        Interpreter interpreter = new Interpreter();
        try {
            System.out.println("Result windowSettings:\n" + genCode);
            FrameModel compiled = (FrameModel) interpreter.eval(genCode);
            assertEquals(compiled.getTitle(), testedObj.getTitle());
            assertEquals(compiled.getMaximumSize(), testedObj.getMaximumSize());
            assertEquals(compiled.getMinimumSize(), testedObj.getMinimumSize());
            assertEquals(compiled.getPreferredSize(), testedObj.getPreferredSize());
            assertEquals(compiled.getSize(), testedObj.getSize());
            assertEquals(compiled.getDisplayMode(), testedObj.getDisplayMode());

            assertNotSame(compiled.getTitle(), defObj.getTitle());
            assertNotSame(compiled.getMaximumSize(), defObj.getMaximumSize());
            assertNotSame(compiled.getMinimumSize(), defObj.getMinimumSize());
            assertNotSame(compiled.getPreferredSize(), defObj.getPreferredSize());
            assertNotSame(compiled.getSize(), defObj.getSize());
            assertNotSame(compiled.getDisplayMode(), defObj.getDisplayMode());
        } catch (EvalError ex) {
            Logger.getLogger(SettingsTestings.class.getName()).log(Level.SEVERE, null, ex);
            Assert.fail("Uncompilable code");
        }
    }

}
